In [1]:
import numpy as np

Notes on Vectorization

Vectorized is Always better. Vectorization is more smooth than looping through the way because of SIMD instructions. Always vectorize your code as much as possible.

You could have 10x - 800x speedups with vectorized code , and in Deep learning for SIMD instructions this is more and more true because

Vectorized = Parallelized

Avoid Explicit For loops. Numpy allows you to use math functions with vectors as well.

Vectorizing Linear Regression :

In Numpy , W.t = Transpose of vector W

Broadcasting :


What broadcasting essentialy does if the Matrices or vectors in an equation or code you've written are of different dimensions if one of the operand is just a row or column vector ( 1m or m1 ) then it copies the vector to equal its dimensions to that of the matrix

cal = A.sum(axis=0)

The above line row squashes the matrix adding them in column wise format. I think squasing by axis =1 would give us a Column Vector .

axis 0 = sum verically
axis 1 = sum columnvise

Reshaping arrays

Two common numpy functions used in deep learning are np.shape and np.reshape().

  • X.shape is used to get the shape (dimension) of a matrix/vector X.
  • X.reshape(...) is used to reshape X into some other dimension.

For example, in computer science, an image is represented by a 3D array of shape $(length, height, depth = 3)$. However, when you read an image as the input of an algorithm you convert it to a vector of shape $(length*height*3, 1)$. In other words, you "unroll", or reshape, the 3D array into a 1D vector.

Exercise: Implement image2vector() that takes an input of shape (length, height, 3) and returns a vector of shape (length*height*3, 1). For example, if you would like to reshape an array v of shape (a, b, c) into a vector of shape (a*b,c) you would do:

v = v.reshape((v.shape[0]*v.shape[1], v.shape[2])) # v.shape[0] = a ; v.shape[1] = b ; v.shape[2] = c
  • Please don't hardcode the dimensions of image as a constant. Instead look up the quantities you need with image.shape[0], etc.

Cal.reshape(1,4)

The above line ie the reshape function changes a list into a matrix.


In [25]:
import math
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

In [30]:
#Z= np.dot(W.t,X)+b

A=[]
z=np.random.randn(5) # dont use randn(5) as far as possible because the shape is (5,) which is a rank 1 vector . 
# instead use randn(5,1) as follows which makes the shape of the vector as columm or row
z=np.random.randn(5,1)
A= 1/(1+np.exp(-z))
print(A)


[[ 0.22891177]
 [ 0.49965043]
 [ 0.12537146]
 [ 0.66175469]
 [ 0.14231843]]

In [ ]: